package com.loadmore.common.widget

import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import android.util.AttributeSet
import android.view.Gravity
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import com.loadmore.common.R

/**
 * @author sunleilei
 * @date 2018/7/12
 * @time 16:19
 */
class AppToolbar : Toolbar {
    public lateinit var centerLayout:LinearLayout
    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init()
    }

    private fun init() {
        title = ""

        centerLayout = LinearLayout(context)
        centerLayout.orientation = LinearLayout.HORIZONTAL
        var layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT)
        layoutParams.gravity = Gravity.CENTER
        addView(centerLayout,layoutParams)
    }
    public fun setCenterTitle(string: String?) {
        var textView:TextView? = centerLayout.findViewById(R.id.webview_title)
        if (textView == null) {
            textView = getTextView()
            centerLayout.addView(textView)
        }
        textView.text = string
        textView.gravity = Gravity.CENTER
    }

    private fun getTextView():TextView {
        var textView = TextView(context)
        textView.id = R.id.webview_title
        textView.setSingleLine()
        textView.maxEms = 10
        textView.textSize = 18.0f
        textView.setTextColor(Color.WHITE)
        textView.ellipsize = TextUtils.TruncateAt.END
        return textView
    }

}
